/*
 * Copyright 2010 Mark Eschbach.
 *
 * $HeadURL$
 * $Id$
 */
package com.meschbach.cise.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 *
 * @author "Mark Eschbach" &lt;meschbach@gmail.com&gt;
 */
public class FileLength {

    File file;

    public FileLength(File file) {
        this.file = file;
    }

    public long findLength() throws IOException {
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bif = new BufferedInputStream(fis);
        try {
            byte buffer[] = new byte[1024];
            long size = 0;

            boolean keepReading = true;
            while (keepReading) {
                long read = bif.read(buffer);
                if (read == -1) {
                    keepReading = false;
                } else {
                    size += read;
                }
            }
            return size;
        } finally {
            bif.close();
        }
    }

    public static long size(File file) throws IOException {
        return new FileLength(file).findLength();
    }
}
